
// 添加车辆跳转
$('.add').on('click',()=>{
  window.location.href = "{:url('html/car/models_add')}"
})
var page = 1;
var pagenum = 10;
var fi=1;
var url={
  list:api.carModel.typeList,
  delete:api.carModel.deletetype,
  edit:api.carModel.edittype
}
// 表头
var main= [[
	{if $Think.get.op}
	{else/}
  {
  	checkbox: true,
  	fixed: true
  },
  {/if}
  {
    field: 'car_type_name',
    title: '车型',
    templet:`<div>{{d.car_type_name?d.car_type_name:"-"}}</div>`,
    width:200,
    fixed: true
  }, {
    field: 'mileage_cost',
    title: '里程费'
  }, {
    field: 'starting_price',
    title: '起步价'
  }, {
    field: 'night_driving_cost',
    title: '夜间行驶费',
    width:110
  }, {
    field: 'low_speed_cost',
    title: '低速行驶费',
    width:110
  }, {
    field: 'time_charge',
    title: '时长费'
  }, {
    field: 'transit_cost',
    title: '远途费'
  }, {
    field: 'waiting_cost',
    title: '候客费'
  }, {
    field: 'cancel_cost',
    title: '取消费'
  }, {
    field: 'chartered_km',
    title: '包车里程',
    templet:'<div>{{d.chartered_km?d.chartered_km:"-"}}</div>',
    width:110
  }, {
    field: 'chartered_hour',
    title: '包车小时',
    templet:'<div>{{d.chartered_hour?d.chartered_hour:"-"}}</div>',
    width:100
  },{
    field: 'p_chartered_charge',
    title: '包车费用',
    templet:'<div>{{d.p_chartered_charge?d.p_chartered_charge:"-"}}</div>',
    width:100
  }, {
    field: 'p_chartered_overtime',
    title: '超时价(元/分钟)',
    templet:'<div>{{d.p_chartered_overtime?d.p_chartered_overtime:"-"}}</div>',
    width:110
  }, {
    field: 'state',
    title: '状态',
    templet:'<div><div style="color:{{d.state==9?"red":"green"}}">{{d.state==9?"禁用":"已启用"}}</div></div>'
  }
  {if $Think.get.op}
  ,{
    field: 'right',
    title: '操作',
    align: 'center',
    toolbar: '#barDemo2',
    fixed: 'right',
    minWidth:200
  }]
	{else/}
  ,{
    field: 'right',
    title: '操作',
    align: 'center',
    toolbar: '#barDemo',
    fixed: 'right',
    minWidth:200
  }]
	{/if}
];
var data={};
data.page=page;
data.pagenum=pagenum;
var where_json ={};
{if $Think.get.type}
where_json.type=['in','{$Think.get.type}']
{/if}
{if $Think.get.car_type}
where_json.car_type=['in','{$Think.get.car_type}']
{/if}

// {if $Think.get.filter}
// where_json.plate_no=['in','{$Think.get.plate_no}']
// {/if}

data.where_json=JSON.stringify(where_json)
console.log(data)
console.log(where_json)
ajaxmodels(data);
function ajaxmodels(data) {
  Load();
  //请求数据
  ajax(url.list,data, 'post').done(function (res) {
      Load(1);
      // 成功回调
      //方法级表单渲染
      console.log(res)
      if(res.code == 1){
      table.render({
          elem: '#models',
          cols: main,
          data: res.data,
          limit: pagenum,
          cellMinWidth: 80
      });
      if (fi == 1) {
          laypage.render({
              elem: 'page',
              count: res.count,
              layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
              theme: '#1E9FFF',
              jump: function (obj, first) {
                  console.log(obj)
                  if (!first) {
                      page = obj.curr;
                      data.page = page;
                      pagenum = obj.limit;
                      data.pagenum = pagenum;
                      ajaxmodels(data);
                  } else {

                  }
              }
          })
          fi = 2;
      }
      // 操作
      table.on('tool(barDemo)', function (obj) {
                  var data1 = obj.data; //获得当前行数据
                  var data2 = {
                      ids:data1.car_type_id
                  };
                  var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                  var tr = obj.tr; //获得当前行 tr 的DOM对象
                  if (layEvent === 'edit') {
                      window.location.href = "{:url('/html/car/models_add')}?car_type_id="+data1.car_type_id;
                  } else if (layEvent === 'del') {
                      layer.confirm('是否删除', function (index) {
                              Load()
                              ajax(url.delete, data2, 'post').done(function (res) {
                                  // 成功回调
                                  console.log(res)
                                  if(res.code ==1){
                                    layer.closeAll();
                                  Msg('删除成功');
                                  ajaxmodels(data)
                                  }else{
                                    Err('请求失败,稍后再试')
                                  }
                                  
                              }).fail(function (err) {
                                  // 失败回调
                                  layer.close(deleteload);
                                  Err(res.msg ? res.msg : '操作失败，稍后再试');
                              });
                              layer.close(index);
                          })
                      }else if( layEvent == 'forbidden'){
                            console.log('禁用')
                            layer.confirm('是否禁用', function (index) {
                              Load()
                              data1.state =9;
                              ajax(url.edit, data1, 'post').done(function (res) {
                                  // 成功回调
                                  console.log(res)
                                  layer.closeAll();
                                  Msg('禁用成功');
                                  ajaxmodels(data)
                              }).fail(function (err) {
                                  // 失败回调
                                  layer.close(deleteload);
                                  Err(res.msg ? res.msg : '操作失败，稍候再试');
                              });
                              layer.close(index);
                          })                          
                      }else if( layEvent == 'startusing'){
                            console.log('启用')
                            layer.confirm('是否启用', function (index) {
                              Load()
                              data1.state =1;
                              ajax(url.edit, data1, 'post').done(function (res) {
                                  // 成功回调
                                  console.log(res)
                                  layer.closeAll();
                                  Msg('禁用成功');
                                  ajaxmodels(data)
                              }).fail(function (err) {
                                  // 失败回调
                                  layer.close(deleteload);
                                  Err(res.msg ? res.msg : '操作失败，稍候再试');
                              });
                              layer.close(index);
                          })
                      }else if( layEvent == 'choose'){
                      	var index = parent.layer.getFrameIndex(window.name);
                           parent.$('.car_type_id').val(data1.car_type_id)
                           parent.$('.car_type_name').val(data1.car_type_name)
                           parent.layer.close(index);
                      }
                  })
                //删除多个
        $(".delete-multiple").on('click', function () {
          var car_type_ids = '';
          var checkStatus = table.checkStatus('models'); //test即为基础参数id对应的值
          console.log(checkStatus.data) //获取选中行的数据
          console.log(checkStatus.data.length) //获取选中行数量，可作为是否有选中行的条件
          console.log(checkStatus.isAll) //表格是否全选
          if(checkStatus.data.length){
          for (var dei = 0; dei < checkStatus.data.length; dei++) {
            if (dei == 0) {
              car_type_ids += checkStatus.data[dei].car_type_id
            } else {
              car_type_ids += ("," + checkStatus.data[dei].car_type_id)
            }
          }
                  console.log(car_type_ids)
                  var data2 ={
                      ids : car_type_ids
                  }
          layer.open({
            type: 1,
            title: false,
            id: "delete",
            content: '<div style="padding: 20px 100px;">是否删除</div>',
            btn: ['确定', '取消'],
            btnAlign: 'c',
            shade: 0,
            yes: function () {
              var deleteload = layer.load(1, {
                shade: [0.1, '#fff'] //0.1透明度的白色背景
                          });
                          
                          ajax(url.delete,data2,'post').done(function(res) {
                              // 成功回调
                                layer.close(deleteload);
                                      layer.closeAll();
                                      Msg('删除成功');
                                      ajaxmodels(data)
                              }).fail(function(err) {
                              // 失败回调
                              layer.close(deleteload);
                  Err(res.msg ? res.msg : '操作失败，稍候再试');
                              });
            },
            btn2: function () {
              layer.closeAll();
            }
          });
        }else{
          Err('未有选择项')
        }
        });
  }else{
      Err(res.msg)
  }
  }).fail(function (err) {
      // 失败回调
      Load(1)
      Err('请求失败')
  });
};
